import { sign, getSignNotify, signNotify } from "@/api/task/sign.service";
import { showText } from "../../utils/util";

Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    TaskList: {
      type: Array,
      value: []
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    showDetail: false,
  },
  attached() {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    cancel() {
      this.triggerEvent('close')
    },

    /** 点击积分任务弹窗的金色按钮 */
    toTask(e) {
      const item = e.currentTarget.dataset.item;
      if (item.name == '每日签到') {
        if (item.status)
          getSignNotify().then(res => {
            if (res.code == 200) {
              if (res.data) showText("您已订阅，无需重复操作");
              else wx.requestSubscribeMessage({
                tmplIds: ['pSjssHNRmgB9_NjzVTxOTteMrAcR3BjvlQe23WC6OVM'],
                success(res) {
                  signNotify().then(res => {
                    if (res.code == 200) { showText("订阅成功"); }
                  });
                },
                fail(res) { showText("订阅失败"); }
              })
            }
            else showText(res.msg);
          });
        else sign().then(res => {
          if (res.code == 200) {
            showText(`签到成功，获得${res.data.integral}积分！`);
            const newPointTaskList = [...this.properties.TaskList];
            newPointTaskList[0].status = true;
            this.triggerEvent('update', newPointTaskList);
          }
          else showText(res.msg);
        })
      }
      else if (item.url) {
        this.cancel();
        wx.navigateTo({ url: item.url })
      } else showText(item.noUrlTip || "敬请期待")
    },
  }
})